import { defineStore } from 'pinia'
import { ref } from 'vue'

export const useStudentStore = defineStore('student', () => {
  // 当前选中的学生
  const selectedStudents = ref([])
  // 临时存储选中的学生（用于取消操作）
  const tempSelectedStudents = ref([])
  // 当前编辑的学生
  const currentStudent = ref(null)

  // 设置选中的学生
  const setSelectedStudents = (students) => {
    selectedStudents.value = students
  }

  // 保存当前选中的学生，用于后续恢复
  const saveSelection = () => {
    tempSelectedStudents.value = [...selectedStudents.value]
  }

  // 取消选择，返回之前保存的选择状态
  const cancelSelection = () => {
    const temp = [...tempSelectedStudents.value]
    tempSelectedStudents.value = []
    return temp
  }

  // 清除选择
  const clearSelection = () => {
    selectedStudents.value = []
    tempSelectedStudents.value = []
  }

  // 设置当前编辑的学生
  const setCurrentStudent = (student) => {
    currentStudent.value = student ? { ...student } : null
  }

  // 清除当前编辑的学生
  const clearCurrentStudent = () => {
    currentStudent.value = null
  }

  return {
    selectedStudents,
    tempSelectedStudents,
    currentStudent,
    setSelectedStudents,
    saveSelection,
    cancelSelection,
    clearSelection,
    setCurrentStudent,
    clearCurrentStudent
  }
})
